package com.db.transport.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class SessionClientThread extends SessionClient {

	public SessionClientThread(String clientID, Socket socket, BufferedReader reader,
			PrintWriter writer) {
		super(clientID, socket, reader, writer);
	}

	public SessionClientThread(String clientID, Socket socket) throws IOException {
		super(clientID, socket);
	}


	private BlockingQueue<String> messages;
	
	private Thread threadForReadMessage = new Thread(new Runnable() {

		@Override
		public void run() {
			String message = null;
			while (!Thread.interrupted()) {
				try {
					if (reader.ready()) {
						message = reader.readLine();
						messages.put(message);
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}			
		}
		
	});

	{
		messages = new LinkedBlockingQueue<String>();
		threadForReadMessage.start();
	}

	@Override
	public String getMessage() {
		String message = null;
		try {
			message = messages.take();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return message;
	}
}
